//
//  CN_TecentLocation.swift
//  YiZhangGui
//
//  Created by lemon on 2023/4/4.
//


//import Foundation
//import UIKit
//import TencentLBS
//import KeychainAccess
//
//class CN_TecentLocation: NSObject {
//
//    static let shared: CN_TecentLocation = CN_TecentLocation()
//    private override init() {}
//    
//    
//    // 更新经纬度的回调 和 更新详细地址的回调
//    var updateCoordinateAndAddressBlock: ((_ longitude: String, _ latitude: String, _ address: String)->())?
//    var updateCoordinateAndAddressfakeBlock: ((_ longitude: String, _ latitude: String, _ address: String,_ fakeCode:String)->())?
//    
//    var updateCoordinateAndAddressfakeAccurBlock: ((_ longitude: String, _ latitude: String, _ address: String,_ fakeCode:String,_ accuracy:String)->())?
//    
//    var updateCoordinateAndAddressfakeAccurLocationBlock: ((_ longitude: String, _ latitude: String, _ address: String,_ fakeCode:String,_ accuracy:String,_ location:TencentLBSLocation)->())?
//    
//    var locationFailErrorBlock: ((_ error: String)->())?
//    var updateCoordinateAndLocationsBlock: ((_ longitude: String, _ latitude: String, _ location: TencentLBSLocation)->())?
//
//    private var locationManager: TencentLBSLocationManager!
//
//    private var controller: UIViewController?
//    
//    /// 开始定位,最好将startLocation()放在viewWillAppear或viewDidAppear 中开启定位 和 viewWillDisappear 中停止定位
//    func startLocation(Tecent_apiKey: String, controller: UIViewController, msg: String, showAlert: Bool) {
//        // 腾讯
//        if TencentLBSLocationManager.getUserAgreePrivacy() == false {
//            TencentLBSLocationManager.setUserAgreePrivacy(true)
//        }
//        locationManager = TencentLBSLocationManager()
//        locationManager.delegate = self
//        locationManager.enableAntiMockLocation = true
////        locationManager.coordinateType = .GCJ02
////        locationManager.desiredAccuracy = kCLLocationAccuracyBest
//        
//
//        locationManager.apiKey = Tecent_apiKey
//        locationManager.pausesLocationUpdatesAutomatically = false
//        locationManager.requestLevel =  .adminName
//        
//        locationManager.setDeviceId(getDevice_uuid())
//        
//        
//        let authStatus = CLLocationManager.authorizationStatus()
//        if authStatus == CLAuthorizationStatus.notDetermined {
//            locationManager.requestWhenInUseAuthorization()
//        }
//        
////        let locationAuthorization = CLLocationManager.authorizationStatus()
////        switch locationAuthorization {
////        case .authorizedWhenInUse:
////            print("授权使用定位服务，但只在使用应用时。")
////        case .authorizedAlways:
////            print("总是授权使用定位服务，包括应用在后台时。这就是精确定位服务。")
////        case .denied:
////            print("用户已经明确拒绝了应用使用定位服务。")
////        case .notDetermined:
////            print("用户还没有做出选择是否授权应用使用定位服务。")
////        case .restricted:
////            print("定位服务被限制，可能由于活动限制、家长控制等。")
////        @unknown default:
////            print("未知的定位服务授权状态。")
////        }
//        
////        if #available(iOS 14.0, *) {
////            let locationManager = CLLocationManager()
////            let accuracyAuthorization = locationManager.accuracyAuthorization
////            switch accuracyAuthorization {
////            case .fullAccuracy:
////                print("用户已开启精确位置")
////            case .reducedAccuracy:
////                print("用户已关闭精确位置")
////            @unknown default:
////                print("未知的精确位置授权状态。")
////            }
////        } else {
////            // 在 iOS 14以下，无法查询到精确位置的授权状态。
////            print("无法查询到精确位置的授权状态")
////        }
//        
//        
//        self.startUpdatingLocation()
//        
//        self.controller = controller
//    }
//    /// 当手动开启定位后，更新定位
//    func startUpdatingLocation() {
//        locationManager.startUpdatingLocation()
//        
//    }
//    /// 停止定位, 该方法不要放在BaseController的viewDidDisappear()方法中
//    func stopLocation() {
//        if(locationManager != nil){
//            locationManager.stopUpdatingLocation()
//        }
//        
//        
//    }
//    
//    func getDevice_uuid() -> String {
//        let keychain = Keychain(service: "com.DaNon.DaNengXunDian")
//        let user_deviceToken = UserDefaultsManager.queryDataForString(UserDefaultsName.User_deviceToken)
//        if let device_id = keychain[string: "yizhanggui_device_id"] {
//            return device_id
//        } else {
//            keychain[string: "yizhanggui_device_id"] = user_deviceToken
//        }
//        
//        return keychain[string: "yizhanggui_device_id"] ?? user_deviceToken
//    }
//}
//
//extension CN_TecentLocation: TencentLBSLocationManagerDelegate {
//
//    func tencentLBSLocationManager(_ manager: TencentLBSLocationManager, didFailWithError error: Error) {
//        // 定位错误
//        if self.locationFailErrorBlock != nil {
//            self.locationFailErrorBlock!(error.localizedDescription)
//        }
//    }
//    func tencentLBSLocationManager(_ manager: TencentLBSLocationManager, didUpdate location: TencentLBSLocation) {
//        /// 位置更新，获取经纬度
//        if self.updateCoordinateAndAddressBlock != nil {
//            self.updateCoordinateAndAddressBlock!(String(describing: location.location.coordinate.longitude), String(describing: location.location.coordinate.latitude), location.address ?? "")
//        }
//        if updateCoordinateAndLocationsBlock != nil {
//            self.updateCoordinateAndLocationsBlock!(String(describing: location.location.coordinate.longitude), String(describing: location.location.coordinate.latitude), location)
//
//        }
//        
//        if updateCoordinateAndAddressfakeBlock != nil {
//            self.updateCoordinateAndAddressfakeBlock!(String(describing: location.location.coordinate.longitude), String(describing: location.location.coordinate.latitude), location.address ?? ""  ,String(describing:location.fakeCode.rawValue))
//        }
//        
//        if updateCoordinateAndAddressfakeAccurBlock != nil {
//            let  accur = String(location.location.horizontalAccuracy) + "m"
//            self.updateCoordinateAndAddressfakeAccurBlock!(String(describing: location.location.coordinate.longitude), String(describing: location.location.coordinate.latitude), location.address ?? ""  ,String(describing:location.fakeCode.rawValue),accur )
//        }
//        
//        if updateCoordinateAndAddressfakeAccurLocationBlock != nil {
//            let  accur = String(location.location.horizontalAccuracy) + "m"
//            self.updateCoordinateAndAddressfakeAccurLocationBlock!(String(describing: location.location.coordinate.longitude), String(describing: location.location.coordinate.latitude), location.address ?? ""  ,String(describing:location.fakeCode.rawValue),accur ,location)
//        }
//        
//        print((location.address ?? "")  + "经度:" + String(describing: location.location.coordinate.longitude) + "纬度:" + String(describing: location.location.coordinate.latitude))
//        
//        print("location:")
//        print( location.location);
//        print("horizontalAccuracy:")
//        print (location.location.horizontalAccuracy)
////        location.location.accessibilityValue
//        print("name:" )
//        print(location.name as Any)
//        print("address:")
//        print(location.address as Any)
//        
////        let currentDate = Date()
////
////        let dateFormatter = DateFormatter()
////        dateFormatter.dateFormat = "HH:mm:ss"
////        let formattedTime = dateFormatter.string(from: currentDate)
////
////        print(formattedTime)
//        
////        print(location.fakeCode)
//        
//        
////        location.fakeCode
//    }
//
//}
